package com.bsoft.utils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class IoUtil {
    public static void main(String[] args) throws IOException {
        writeFile(); //单个字节写、字节数字写
        readFile1();//单个字节读取
        readFile2();//字节数组读取
        readFile3();//一次性读取
    }
    /** ==================字节流读写:FileOutputStream写文件、FileInputStream读文件=====================================*/

    static void writeFile() throws IOException {
        //1、第一种方法写，单个字节写
        //会自动创建文件，目录不存在会报错， true 表示 追加写，默认是false
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\hello.txt", false);
        //往文件里面一个字节一个字节的写入数据
        fileOutputStream.write((int) 'H');
        fileOutputStream.write((int) 'a');
        fileOutputStream.write((int) 'C');

        //2、第二种方法写 字节数组写
        String s = " HelloCoder";
        //入文件里面一个字节数组的写入文件，文件为UTF_8格式
        fileOutputStream.write(s.getBytes(StandardCharsets.UTF_8));
        //刷新流
        fileOutputStream.flush();
        //关闭流
        fileOutputStream.close();
    }

    static void readFile1() throws IOException {
        //1、第一种读的方法，但字节读
        System.out.println("------一个字节读------");
        //传文件夹的名字来创建对象
        FileInputStream fileInputStream = new FileInputStream("F:\\hello.txt");
        int by = 0;
        //一个字节一个字节的读出数据
        while ((by = fileInputStream.read()) != -1) {
            System.out.print((char) by);
        }
        //关闭流
        fileInputStream.close();
    }

    static void readFile2() throws IOException {
        //2、第二种读的方法，字节数组读
        System.out.println();
        System.out.println("------字节数组读------");
        FileInputStream fileInputStream = new FileInputStream("F:\\hello.txt");
        //通过File对象来创建对象
        fileInputStream = new FileInputStream(new File("F:\\hello.txt"));
        int by = 0;
        byte[] bytes = new byte[10];
        //一个字节数组的读出数据，高效
        while ((by = fileInputStream.read(bytes)) != -1) {
            for (int i = 0; i < by; i++) {
                System.out.print((char) bytes[i]);
            }
        }
        //关闭流
        fileInputStream.close();
    }

    static void readFile3() throws IOException {
        //3、第三种读方法，一次性读
        System.out.println();
        System.out.println("------一次性读文件------");
        FileInputStream fileInputStream = new FileInputStream("F:\\hello.txt");
        fileInputStream = new FileInputStream(new File("F:\\hello.txt"));
        //一次性读文件
        int iAvail = fileInputStream.available();
        int by = 0;
        byte[] bytesAll = new byte[iAvail];
        while ((by = fileInputStream.read(bytesAll)) != -1) {
            for (int i = 0; i < by; i++) {
                System.out.print((char) bytesAll[i]);
            }
        }
        fileInputStream.close();
    }

    /** ==================字符流读写:FileWriter写文件、FileReader读文件=====================================*/
    static void write() throws IOException {
        FileWriter fileWriter = new FileWriter("F:\\Hello1.txt");
        //为防止乱码，可以这样写，字符流和字节流互转, 字节流转字符流防止乱码
//        Writer fileWriter = new BufferedWriter(new OutputStreamWriter(
//                new FileOutputStream("F:\\Hello1.txt"), StandardCharsets.UTF_8));
        fileWriter.write("今天打工你不狠，明天地位就不稳\n" +"今天打工不勤快，明天社会就淘汰");

        // 如果没有刷新，也没有关闭流的话 数据是不会写入文件的
        fileWriter.flush();
        fileWriter.close();
    }

    static void read1() throws IOException {
        System.out.println("------一个一个char读-------");
        FileReader fileReader = new FileReader("F:\\Hello1.txt");
        int ch = 0;
        String str = "";
        //一个一个char读
        while ((ch = fileReader.read()) != -1) {
            str += (char) ch;
        }
        System.out.println(str);
    }

    static void read2() throws IOException {
        System.out.println("------char数组[]读-------");
        FileReader fileReader = new FileReader(new File("F:\\Hello1.txt"));
        int len = 0;
        char[] chars = new char[10];
        while ((len = fileReader.read(chars)) != -1) {
            //这种读有误
//            System.out.print(new String(chars));
            System.out.print((new String(chars, 0, len)));
        }
        fileReader.close();
    }

    /**使用数组读写文件,普通字节流*/
    static void useInputStreamCopyFile() throws IOException {
        File file = new File("F:\\杨超越.png");
        InputStream is = new FileInputStream(file);

        File file2 = new File("F:\\杨超越_copy.png");
        OutputStream os = new FileOutputStream(file2);
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = is.read(bytes)) != -1) {
            os.write(bytes);
        }
        is.close();
        os.close();
    }
    /**使用数组写文件,缓冲字节流,快*/
    static void useBufferenInputStream() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\杨超越.png"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\杨超越_copy2.png"));
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();
    }

    /**普通字节流复制文件*/
    static void useInputStreamCopyFile1() throws IOException {
        File file = new File("F:\\Hello1.txt");
        InputStream is = new FileInputStream(file);

        File file2 = new File("F:\\Hello1_copy1.txt");
        OutputStream os = new FileOutputStream(file2);
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = is.read(bytes)) != -1) {
            os.write(bytes, 0, len);
        }
        is.close();
        os.close();
    }

    /**缓冲字符流读取文件*/
    static void useBufferedReaderCopyFile() throws IOException {
        File file = new File("F:\\Hello1.txt");
        InputStream is = new FileInputStream(file);
        Reader reader = new InputStreamReader(is);
        //创建字符流缓冲区，BufferedReader 的构造入参是一个 Reader
        BufferedReader bufferedReader = new BufferedReader(reader);

        File file2 = new File("F:\\Hello1_copy2.txt");
        OutputStream os = new FileOutputStream(file2);
        Writer writer = new OutputStreamWriter(os);
        //创建字符流缓冲区，BufferedWriter 的构造入参是一个 Writer
        BufferedWriter bufferedWriter = new BufferedWriter(writer);

        String line = null;
        //readLine()方法 是根据\n 换行符读取的
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line);
            //这里要加换行
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }

    /**使用数组读写文件,普通字符流*/
    static void useFileReaderCopyFile() throws IOException {
        //使用FileReader、FileWriter 一步到位
        Reader reader = new FileReader("F:\\Hello1.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);
        Writer writer = new FileWriter("F:\\Hello1_copy3.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}